#include <netinet/in.h>
#include <stdio.h>
#include <iwlib.h>
#include <mcheck.h>

/*
 * TEMP 
 * Discover ip
 */
int set_ip_configuration(char *device, struct ether_addr *mac_addr)
{
	struct in_addr router_ip;
	int result;

	/* if the ip is in the ddbb */
	result = exists_in_ddbb(mac_addr, &router_ip);
	if (result > 0) {
		printf("[tick]I don't need to flood the network\n");
		set_ip(device, router_ip);
		return 0;
	}

	/* if not */
	result = discover_router_address(mac_addr, device, &router_ip);
	if (result != 0) {
		printf("[tick]If was not possible to discover the ip\n");
		return -1;
	}

	/* add the ip to the ddbb */
	result = save_new_addr_in_ddbb(mac_addr, &router_ip);
	if (result != 0) {
		fprintf(stderr, "[tick]Error: saving the ip in the ddbb\n");
	}
	set_ip(device, router_ip);
	return 0;
}

/* deprecated */
void __loop_checking_connection()
{
	int x;
	do {
	       x = check_connec_dns(); /* it can take between 0 and 20 seconds */
	       printf("[tick]Quality of connection : %d\n", x);
	       if (x > 70){ /* if we have good connection then it sleeps a bit */
		       sleep(3);
	       }
	}while (x>0);
}

void loop_checking_connection_v2()
{
	struct in_addr ip;
	/* www.digg.com : 64.191.203.30 */
	inet_aton("64.191.203.30",&ip);
	check_connec_ping(ip); /* it should finish when there is not connection */
}

/******************************* MAIN ********************************/

/*------------------------------------------------------------------*/
/*
 * The main !
 */
int main(int	argc, char **	argv)
{
 	
  mtrace();
		
  char *dev;			/* device name			*/
  wireless_scan *wscan = NULL; /* results from the scanning */
  int wscan_index = -1; /* length of the list wscan */
  int stop = 0;
  int net_index;
  int i;
  wireless_scan *wstmp = NULL;

  int x;
  int result;

  if(argc != 2) {
	  fprintf(stderr, "%s interface\n",argv[0]);
	  return -1;
  }

  dev = argv[1];

    
  /* forever */
  while(1) {
	
	  /* it creates the first element on the list */
	  wscan = (wireless_scan *) malloc(sizeof(wireless_scan));
	  
	  wscan_index = -1;

	  result = scan(dev, wscan, &wscan_index);
	  
	  if (result < 0) {
		  fprintf(stderr, "The device can't perform a scan\n");
		  return -1;
	  }

  	  if (wscan_index < 0){
		  printf("No networks found. Waiting 3 secs\n");
		  sleep(3);
		  free_wscan(wscan,&wscan_index);
		  continue;
	  }
   	  
	  net_index = best_network(wscan, wscan_index);
	  if (net_index < 0) {
		  printf("No networks suitable. Waiting 3 secs\n");
		  sleep(3);
		  free_wscan(wscan,&wscan_index);
		  continue;
	  }
	  printf("the best network to connect is : %d\n", net_index);

	  /* wstmp points to the selected item in the list */
	  wstmp=wscan;
	
	  for (i=0;i<net_index;i++){
		wstmp=wstmp->next;
	  }
	  
	  /* it has a good network. lets connect */
	  result = connect_to(dev, wstmp);  
	  
	  /* TODO: there are memory leaks */
	  if (result == 0) {
		  result = set_ip_configuration(dev, (struct ether_addr*)&wstmp->ap_addr.sa_data);
		  if (result == 0)
			  loop_checking_connection_v2();  /* it finish when there is no connection */
		  /* 
		     else : bad points to that adress */
	  } 
	    
	  /* Free the wscan list */
	  free_wscan(wscan,&wscan_index);
	  break;
  }

  printf("tick finished\n");
  return 0;
}

